/*
Written by Best Flash Solutions, Inc  
See:
	http://www.bestflashsolutions.com
Contact me: contact@bestflashsolutions.com
*/

/**
 * Process PHP request
 */
package com.bestflashsolutions.phpRequest
{
	import com.bestflashsolutions.app.Message;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	public class PHPRequest
	{
		public var params:URLVariables = new URLVariables();
		//PHP path
		public var phpPath: String = "";
		//POST or GET
		public var usingPOST: Boolean = true;
		//Callback function
		public var listenerFn: Function;

		public function PHPRequest(_phpPath:String, _listenerFn:Function)
		{
			phpPath = "http:localhost/"+_phpPath;	
			listenerFn = _listenerFn;	
		}
		
		public function request() {
			//Now send these to PHP send mail script
			var dataRequest:URLRequest = new URLRequest(phpPath);
			if (usingPOST) {
				//Using POST method
				dataRequest.method = URLRequestMethod.POST;				
			}
			else {
				//Using GET method
				dataRequest.method = URLRequestMethod.GET;								
			}
			//attact data
			dataRequest.data = params;
			
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			//Listener for Complete
			urlLoader.addEventListener(Event.COMPLETE, listenerFn);
			 
			try {
				//Start sent to PHP Server script
				urlLoader.load(dataRequest);
			}
			catch (event:Error) {
				Message.ERR_PHP_PATH_INCORRECT.alert();
				return false;
			}
			return true;								
		}
	}
}